<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MlsController
 *
 * @author Poh Nean
 */
class MlsController extends PsController
{

	public $layout = 'manage';

	public function accessRules()
	{
		return CMap::mergeArray(array(
					array('allow',
						'roles' => array('ManageModule.MLS'),
					),
						), parent::accessRules());
	}

	public function actionIndex()
	{
		$agency = $this->loadAgency();
		
		if ($agency->settings == null) {
			$agency->settings = array();
		}

		if (!empty($_POST['AgencySetting'])) {
			
			$upload = CUploadedFile::getInstance($agency, 'listing_defalut_image');
			$userfile = null;
			
			if($upload) {
				$userfile = $agency->saveListingDefaultImage($upload);
				if(isset($userfile)) {
					$newUrl = $userfile->getUrl(false);
					$agency->setSetting('MlsDefaultListingImageUserFileId', $userfile->id);
				}
			}
			
			if(isset($newUrl)) {
				$agency->setSetting('MlsDefaultListingImage', $newUrl);
			} else {
				$agency->setSetting('MlsDefaultListingImage', '/images/no-image.png');
			}
			
			foreach ($_POST['AgencySetting'] as $key => $value) {
				$agency->setSetting($key, $value);
			}

			if ($agency->save(true, array('settings'))) {
				PsSystemMessage::add('Your changes have been saved.', 'success');
				$this->refresh();
			}
		}
		
		if($agency->getSetting('MlsDefaultListingImage')) {
			$agency->listing_defalut_image = $agency->getSetting('MlsDefaultListingImage');
		}

		$data = array();
		$data['agency'] = $agency;
		$data['sectors'] = PropertyType::model()->findAllByAttributes(array('parent_id' => null));
		$this->render('index', $data);
	}

	public function loadAgency()
	{
		return Yii::app()->user->getSelectedAgency();
	}

	public function actionAttrSetting()
	{
		$property_exception_list = array('id', 'uuid', 'show_street', 'prop_address_1', 'prop_address_2',
			'prop_address_formatted', 'display_address', 'display_format', 'user_id', 'property_project_id',
			'inventory_unit_type_id');

		$proerty = new Property;
		$mlsListing = new MlsListing;

		$data = array();
		$data['module'] = $this->module->name;
		$data['controller'] = $this->getUniqueId();
		$data['property'] = $proerty;
		$data['mlsListing'] = $mlsListing;
		$data['property_exception_list'] = $property_exception_list;
		$this->render('attrsetting', $data);
	}

}

?>
